const fs = require('fs');
const path = require("path");
const mime = require("mime");
class Common{
	constructor(req, res, config){
		this.res = res;
		this.req = req;
		this.config = config;
	}
	img(filename){
		let file_path = this.config.dir.get("root")+"/"+this.config.dir.get("image");
			file_path+= "/"+filename;
			file_path = path.normalize(file_path);
		this.render(file_path);
	}
	icon(filename){
		let file_path = this.config.dir.get("root")+"/"+this.config.dir.get("public");
			file_path+= "/"+this.config.dir.get("appname")+"/images";
			file_path+= "/"+filename;
			file_path = path.normalize(file_path);
		this.render(file_path);
	}
	style(filename){
		let file_path = this.config.dir.get("root")+"/"+this.config.dir.get("public");
			file_path+= "/"+this.config.dir.get("appname")+"/styles";
			file_path+= "/"+filename;
			file_path = path.normalize(file_path);
		this.render(file_path);
	}
	script(){

	}
	download(){

	}
	upload(){

	}
	render(file_path){
		let comm = this;
		fs.readFile(file_path, function(err,data){
			if (err) {
				comm.res.statusCode = 404;
				comm.res.setHeader("Content-Type", "text/html");
				comm.res.end("");
			} else {
				comm.res.statusCode = 200;
				comm.res.setHeader("Content-Type", mime.lookup(file_path));
				comm.res.end(data);
			}

		})
	}

}

module.exports = Common;